#!/bin/bash -e

log=.log.`date +%N`
echo `basename $0` $@ >>$log || exit 0;
prog=`basename $0`
args=$@

function info() {
cat /mnt/ilustre/app/medical/tools/script/info.snp.txt
rm $log
exit 65
}

function error() {
echo `basename $0`': invalid option -?'
info
}


while getopts  ":a:s:i:l:d:gkLGnc:Ap:m:q:r:t:" opts
do
        case  $opts  in
		a) cutx=$OPTARG;;
        i) interval=$OPTARG;i=i;;
		l) target=$OPTARG;f=f;;
		g) single_cell=T;;
		G) somatic=T;;
		d) desti=$OPTARG;;
		k) mark=T;;
		n) cutadaptor=$OPTARG;n=n;;
		c) cnv=$OPTARG;c=c;;
		r) realn_interval=$OPTARG;;
		m) mem=$OPTARG;m=m;;
		p) cpu=$OPTARG;p=p;;
		q) query=$OPTARG;q=q;;
		A) a=n;;
		s) sample_name=$OPTARG;;
		L) run_local=T;;
		t) continue_dir=$OPTARG;;
		h) info;;
		\?) error;;
        esac
done
shift $(($OPTIND - 1))

if [ -z "$2" ]; then info; fi

if test -z "$sample_name"; then sample_name=sample1; fi

if [ -z "$desti" ]; then desti=snp; fi
if [ -z "$cutx" ]; then cutx=malbac; fi

if test -z "$run_local"; then
	if test -z "$cpu"; then
		if test -z "$mem"; then
			subq.sh -n$sample_name -q$query $prog -L $args
		else
			subq.sh -n$sample_name -m$mem -q$query $prog -L $args
		fi
	else
		if test -z "$mem"; then
			subq.sh -n$sample_name -p$cpu -q$query $prog -L $args
		else
			subq.sh -n$sample_name -m$mem -p$cpu -q$query $prog -L $args
		fi
	fi

clear
echo A snp calling and annotion job, name $sample_name, submitted to pbs...
exit 0
fi

r1=`rel_abs.sh $1`
r2=`rel_abs.sh $2`
r12=`echo $r1 $r2`

if test -z "$continue_dir"; then
makedir=true
else
echo no make dir
cd $continue_dir
fi


. $var


if test "$cutx" = "malbac"; then
	cutadp=cutadp_malbac_170214.sh
elif test "$cutx" = "general"; then
	cutadp=cutadp_170214.sh
fi


if [ "$interval" = "brca12" ]; then
	cutadp=cutadp_170214.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-102X-Covered.b37.bed
elif [ "$interval" = "brca44" ]; then
	cutadp=cutadp_170214.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-001X-Covered.b37.bed
elif [ "$interval" = "ctc80" ]; then
	cutadp=cutadp_ctc80_discard_trimmed.sh
	target=$data_path/intervals/1/ctc80.bed
elif [ "$interval" = "brca12mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/brca12_mor.bed
elif [ "$interval" = "ca50mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/ca50_mor.bed
elif [ "$interval" = "exomes" ]; then
	target=$data_path/intervals/1/b37.agilent_Exon_V5_UTRs.bed
elif [ "$interval" = "wg" ]; then
	target=$data_path/intervals/1/b37.bed
else
	cutadp=cutadp_170214.sh
fi


target=`rel_abs.sh $target`
echo
echo $target
echo $cutadp


snapshot=$out_prefix.snapshot.txt
test -e $snapshot || echo -e "Sample: $sample_name\nRaw data: $r12\nPanel: $interval\nResults: ${http}/$desti/" >$snapshot

if test -n "$cpu" -a $cpu -gt 1; then
# if test $cpu  -gt 1; then
ts0=`echo ${cpu_num}-3|bc`
else
ts0=1
fi

# fastq stat
fastqstat=fastqstat.sh
if
cat $pid_log|grep "$fastqstat $r12 done" >/dev/null; then
echo $fastqstat $r12 done
else
$fastqstat -s$sample_name -t2 -p$out_prefix $r12 &
fi


# fastq stat
fastqc=fastqc.sh
if
cat $pid_log|grep "$fastqc $r12 done" >/dev/null; then
echo $fastqc $r12 done
else
$fastqc -s$sample_name $r12 &
fi

# cutadp
if
cat $pid_log|grep "$cutadp $r12 done" >/dev/null; then
echo $cutadp $r12 done
else
$cutadp -p$out_prefix $r12
fi

# alignment
aln=aln_bwa_bt.sh
if
cat $pid_log|grep "$aln $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq done" >/dev/null; then
echo $aln $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq done
else
$aln -t$ts0 -s$sample_name $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq && rm $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq
fi

# sam2b
sam2b=sam2b_samtools.sh
if
cat $pid_log|grep "$sam2b $out_prefix.sam done" >/dev/null; then
		echo $sam2b $out_prefix.sam done
	else
		$sam2b -t$ts0 $out_prefix.sam
fi


# markdup
if test "$mark" = "T"; then
deldup=deldup.sh
	if
	cat $pid_log|grep "$deldup $out_prefix.sort.bam done" >/dev/null; then
	echo $deldup $out_prefix.sort.bam done
	else
		# delete symbolic link if exists.
		if test -e $out_prefix.markdup.bam; then
			rm $out_prefix.markdup.bam*;
		fi
		
		$deldup -m $out_prefix.sort.bam
	fi

else
	ln -fs $out_prefix.sort.bam $out_prefix.markdup.bam
	if test -e $out_prefix.sort.bam.bai; then
		ln -fs $out_prefix.sort.bam.bai $out_prefix.markdup.bam.bai
	elif test -e $out_prefix.sort.bai; then
		ln -fs $out_prefix.sort.bai $out_prefix.markdup.bai
	fi
fi


# cnv
if test -n "$cnv"; then

	if test "$cnv" = "hmmcp"; then

	deldup=deldup.sh
	if
	cat $pid_log|grep "$deldup $out_prefix.sort.bam done" >/dev/null; then
	echo $deldup $out_prefix.sort.bam done
	else
	$deldup $out_prefix.sort.bam
	fi

	hmmcopy0917b38.sh -w50000 -b37 $out_prefix.deldup.bam
	hmmcopy0917b38.sh -w200000 -b37 $out_prefix.deldup.bam
	hmmcopy0917b38.sh -w1000000 -b37 $out_prefix.deldup.bam
	fi
fi

# realignment
realn=realign_bt.sh
if
	cat $pid_log|grep "$realn $out_prefix.markdup.bam done" >/dev/null; then
	echo $realn $out_prefix.markdup.bam done
else

	if test -n "$realn_interval"; then

		if test -n "$target"; then
			echo realign with known interval $realn_interval in target $target
			$realn -r$realn_interval -i$target $out_prefix.markdup.bam
		else
			$realign_bt -r$realn_interval $out_prefix.markdup.bam
		fi

	elif test -n "$target"; then
		echo realign in $target
		$realn -i$target $out_prefix.markdup.bam
	else
		echo realign in whole region
		$realn $out_prefix.markdup.bam
	fi

fi

# recalibration
recal=recal.sh
if
cat $pid_log|grep "$recal done" >/dev/null; then
echo $recal done
else

recal.sh -p1 -i$target
fi

# bam stat
if
cat $pid_log|grep "$fastqc $out_prefix.realn.recal.bam done" >/dev/null; then
echo $fastqc $out_prefix.realn.recal.bam done
else
fastqc.sh -s$sample_name $out_prefix.realn.recal.bam &
fi

# calling 
cal0=ug.sh
if
cat $pid_log|grep "$cal0 $out_prefix.realn.recal.bam done" >/dev/null; then
echo $cal0 $out_prefix.realn.recal.bam done
else
	if test -n "$somatic"; then
		echo somatic or low coverage
        if test -n "$single_cell"; then
            echo single cell
            $cal0 -G1 -p$out_prefix -i$target $out_prefix.realn.recal.bam
        else
            $cal0 -G -p$out_prefix -i$target $out_prefix.realn.recal.bam
		fi
	else
        if test -n "$single_cell"; then
            $cal0 -1 -p$out_prefix -i$target $out_prefix.realn.recal.bam
        else
            $cal0 -p$out_prefix -i$target $out_prefix.realn.recal.bam
        fi
	fi
fi

# summary
summ=summ.sh
if
cat $pid_log|grep "$summ $out_prefix.realn.recal.bam $target done" >/dev/null; then
echo $summ $out_prefix.realn.recal.bam $target done
else
$summ -g -s$sample_name $out_prefix.realn.recal.bam $target #can not background

ln $out_prefix.gene_summ.txt gene_summ.txt
fi

# anno
anno=anno_0216.sh
if
	cat $pid_log|grep "$anno $out_prefix.snp.vcf $out_prefix.indel.vcf done" >/dev/null; then
	echo $anno $out_prefix.snp.vcf $out_prefix.indel.vcf done
else

	if test -n "$somatic"; then
		echo somatic
		if test -n "$single_cell"; then
			echo;echo;echo single_cell
			$anno -m -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
		else
			$anno -m -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
		fi
	else
		echo germline
		if test -n "$single_cell"; then
			echo;echo;echo single_cell
			$anno -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
		else
			$anno -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
		fi
	fi

fi

# format
format_simple=format_top_simple.sh
if
cat $pid_log|grep "$format_simple done" >/dev/null; then
echo $format_simple done
else
format_top_simple.sh &
fi

format=format_top_0222.sh
if
cat $pid_log|grep "$format $out_prefix.high.vcf $out_prefix.low.vcf done" >/dev/null; then
echo $format $out_prefix.high.vcf $out_prefix.low.vcf done
else
$format $out_prefix.high.vcf $out_prefix.low.vcf
fi



wait

# rename 
rename=file_rename_0222.sh
if
cat $pid_log|grep "$rename $sample_name done" >/dev/null; then
echo $rename $sample_name done
else
$rename $sample_name
fi


echo;echo; echo desti: $desti.

# backup
bk.sh -t$desti ${sample_name}*simple_info*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*.html .log* ${sample_name}*deleterious.txt ${sample_name}*drugs-prediction*



bk_zip.sh ${sample_name}*simple_info*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*2*.html ${sample_name}*drugs-prediction.html



mail.sh $sample_name $interval $desti/`basename $(pwd)` $r12


. $cmd_done
